const query = require("../db/mysql")
const { success, error } = require("../module/module")

class Market {
    //获取楼市圈数据
    getMarket = async (ctx) => {
        let sql = 'select * from housingMarket where 1=1  '
        let data = await query(sql)
        ctx.body = success(data)
    }

    //添加楼市圈
    addMarket = async (ctx) => {
        let { name, userImg, title, img, date } = ctx.request.body
        let sql = `select * from housingMarket where 1=1 and title like "%${title}%" and name like "%${name}%" `
        let data = await query(sql)
        if (!data.length) {
            let sqlData = `insert into housingMarket (name, userImg, title, img, date) values ("${name}","${userImg}","${title}","${img}","${date}")`
            let result = await query(sqlData)
            if (result.affectedRows === 1) {
                ctx.body = success({
                    result,
                }, '添加成功')
            } else {
                ctx.body = error('添加失败')
            }
        } else {
            ctx.body = error('已存在此数据')
        }
    }
    //删除楼市圈
    deleteMark = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `delete from housingMarket where id=${id} `
        let data = await query(sql)

        if (data.affectedRows === 1) {
            ctx.body = success(data, '删除成功')
        } else {
            ctx.body = error("没有此数据，删除失败！")
        }
    }
}

module.exports = new Market()